﻿using System.Collections.Generic;
using System.ComponentModel;
using VNet.VisiSuite.Common.Security;
using VNet.VisiSuite.Service.Contact.Message.Base;
using VNet.VisiSuite.Service.Contact.Message.Item;
using VNet.VisiSuite.Service.Proxy;

namespace VNet.VisiSuite.VisiAdmin.Providers
{
    public class ProductionPlanProviders
    {
        public static void GetProductionPlanReference(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;

            if (worker != null && worker.CancellationPending == false)
            {
                using (var client = new VNetVisiSuiteContractClient())
                {
                    var request = new GetDataRequest
                    {
                        Identity = UserTransportToken.UserIdentity,
                        Id = (int)e.Argument
                    };
                    e.Result = client.GetProductionPlanReference(request);
                }
            }
        }

        public static void GetProductionPlans(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;

            if (worker != null && worker.CancellationPending == false)
            {
                using (var client = new VNetVisiSuiteContractClient())
                {
                    var request = new GetDataRequest
                    {
                        Identity = UserTransportToken.UserIdentity,
                        Id = (int)e.Argument
                    };
                    e.Result = client.GetProductionPlans(request);
                }
            }
        }

        public static void DeleteProductionPlan(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;

            if (worker != null && worker.CancellationPending == false)
            {
                using (var client = new VNetVisiSuiteContractClient())
                {
                    var request = new DeleteDataRequest
                    {
                        Identity = UserTransportToken.UserIdentity,
                        DataId = (int)e.Argument,
                    };
                    e.Result = client.DeleteProductionPlan(request);
                }
            }
        }

        public static void ImportProductionPlan(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;

            if (worker != null && worker.CancellationPending == false)
            {
                var productItems = e.Argument as List<ProductionPlanItem>;

                using (var client = new VNetVisiSuiteContractClient())
                {
                    var request = new SaveDataRequest<IList<ProductionPlanItem>>
                    {
                        Identity = UserTransportToken.UserIdentity,
                        Data = productItems,
                    };
                    e.Result = client.ImportProductionPlans(request);
                }
            }
        }

        public static void InsertProductionPlan(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;

            if (worker != null && worker.CancellationPending == false)
            {
                var productItem = e.Argument as ProductionPlanItem;

                using (var client = new VNetVisiSuiteContractClient())
                {
                    var request = new SaveDataRequest<ProductionPlanItem>
                    {
                        Identity = UserTransportToken.UserIdentity,
                        Data = productItem,
                    };
                    e.Result = client.InsertProductionPlan(request);
                }
            }
        }

        public static void UpdateProductionPlan(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;
            e.Result = null;

            if (worker != null && worker.CancellationPending == false)
            {
                var productItem = e.Argument as ProductionPlanItem;

                using (var client = new VNetVisiSuiteContractClient())
                {
                    var request = new SaveDataRequest<ProductionPlanItem>
                    {
                        Identity = UserTransportToken.UserIdentity,
                        Data = productItem,
                    };
                    e.Result = client.UpdateProductionPlan(request);
                }
            }
        }
    }
}
